package ai4games_tests.soccer;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.fsm.Dispatcher;
import game2dai.fsm.State;
import game2dai.fsm.Telegram;
import game2dai.steering.SBF;

public class PlayerLeavesPitch  extends State implements Constants {

	private static PlayerLeavesPitch instance = null;

	public static PlayerLeavesPitch state(){
		if(instance == null){
			instance = new PlayerLeavesPitch();
			instance.name = "Leave Pitch";
		}
		return instance;
	}

	@Override
	public void enter(BaseEntity owner) {
		PlayerBase player = (PlayerBase) owner;
		player.targetPos.set(player.offFieldPosition);
		player.heading(Team.offPitchHeading);
		player.AP().arriveOn(player.targetPos, SBF.FAST);
	}

	@Override
	public void execute(BaseEntity owner, double deltaTime, World world) {
		PlayerBase player = (PlayerBase) owner;
		if(player.isAtTarget()){
			Dispatcher.dispatch(0, player.ID(), player.team.ID(), AT_TARGET);
			player.moveTo(player.targetPos);
			player.FSM().changeState(PlayerWait.state());
		}
	}

	@Override
	public void exit(BaseEntity owner) {
		PlayerBase player = (PlayerBase) owner;
		player.AP().arriveOff();
//		player.SB().setBehaviour(SBF.ARRIVE, player.targetPos);
	}

	@Override
	public boolean onMessage(BaseEntity owner, Telegram tgram) {
		return false;
	}
	
}
